package com.yitao.cms.service.activity;

import com.google.common.collect.Lists;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.product.ProductGroupActivityDetailDto;
import com.yitao.client.model.product.ProductGroupActivityDetailModel;
import com.yitao.client.model.product.ProductSkuModel;
import com.yitao.client.model.product.QProductGroupActivityDetailModel;
import com.yitao.cms.service.product.ProductCmsSkuService;
import com.yitao.cms.vo.activity.CmsProductGroupActivityDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CmsProductGroupActivityDetailService extends GenericEntityService<ProductGroupActivityDetailModel, ProductGroupActivityDetailDto, QProductGroupActivityDetailModel> {
    public CmsProductGroupActivityDetailService(EntityManager entityManager) {
        super(entityManager);
    }


    @Autowired
    private ProductCmsSkuService productCmsSkuService;

    /**
     * 修改直接删除全部的重新添加
     *
     * @param activityId
     */
    @Transactional
    public void deleteByActivityId(Long activityId) {
        if (activityId != null) {
            long execute = queryFactory.delete(Q).where(Q.activityId.eq(activityId)).execute();
            System.out.println(execute);
        }
    }

    /**
     * @param cmsProductGroupActivityDetailVo
     */
    @Transactional
    public void addProductGroupActivityDetail(CmsProductGroupActivityDetailVo cmsProductGroupActivityDetailVo) {
        if (cmsProductGroupActivityDetailVo != null && cmsProductGroupActivityDetailVo.getActivityId() != null && cmsProductGroupActivityDetailVo.getId() != null) {
            ProductGroupActivityDetailModel productGroupActivityDetailModel = new ProductGroupActivityDetailModel();
            BeanUtils.copyProperties(cmsProductGroupActivityDetailVo, productGroupActivityDetailModel);
            ProductSkuModel productSkuModel = productCmsSkuService.getProductSkuDtoById(cmsProductGroupActivityDetailVo.getId());
            if (productSkuModel != null) {
                productGroupActivityDetailModel.setProductSkuCode(productSkuModel.getProductSkuCode());
                StringBuffer sb = new StringBuffer();
                if (productSkuModel.getAttrSpecName() != null) {
                    sb.append(productSkuModel.getAttrSpecName());
                }
                if (productSkuModel.getAttrSpecValue() != null) {
                    sb.append("/").append(productSkuModel.getAttrSpecValue());
                }
                productGroupActivityDetailModel.setIsDelete(false);
                productGroupActivityDetailModel.setProductSkuId(cmsProductGroupActivityDetailVo.getId());
                productGroupActivityDetailModel.setProductSkuName(sb.toString());
                productGroupActivityDetailModel.setSalesPrice(productSkuModel.getSalesPrice());
                if (cmsProductGroupActivityDetailVo.getActivePrice() == null) {
                    cmsProductGroupActivityDetailVo.setActivePrice(new BigDecimal(0L));
                }
                productGroupActivityDetailModel.setActivePrice(cmsProductGroupActivityDetailVo.getActivePrice().multiply(new BigDecimal(100)).longValue());
            }
            repository.save(productGroupActivityDetailModel);

        }

    }

    /**
     * 根据活动ID查询列表
     *
     * @param activityId
     * @return
     */
    public List<CmsProductGroupActivityDetailVo> getListByActivityId(Long activityId) {
        List<ProductGroupActivityDetailModel> productGroupActivityDetailModelList = queryFactory.select(Q).from(Q).where(Q.activityId.eq(activityId)).fetch();
        List<CmsProductGroupActivityDetailVo> cmsProductGroupActivityDetailVoList = Lists.newArrayList();
        if (productGroupActivityDetailModelList != null && productGroupActivityDetailModelList.size() != 0) {
            CmsProductGroupActivityDetailVo cmsProductGroupActivityDetailVo = null;
            for (ProductGroupActivityDetailModel productGroupActivityDetailModel : productGroupActivityDetailModelList) {
                cmsProductGroupActivityDetailVo = new CmsProductGroupActivityDetailVo();
                BeanUtils.copyProperties(productGroupActivityDetailModel, cmsProductGroupActivityDetailVo);
                if (productGroupActivityDetailModel.getActivePrice() == null) {
                    productGroupActivityDetailModel.setActivePrice(0L);
                }
                cmsProductGroupActivityDetailVo.setActivePrice(new BigDecimal(productGroupActivityDetailModel.getActivePrice()));

                cmsProductGroupActivityDetailVoList.add(cmsProductGroupActivityDetailVo);
            }
        }
        return cmsProductGroupActivityDetailVoList;
    }
}
